import ctypes
import sys
import os

# 加载共享库
if sys.platform == "linux" or sys.platform == "darwin":
    lib = ctypes.CDLL(os.path.abspath("libexample.so"))
elif sys.platform == "win32":
    lib = ctypes.CDLL(os.path.abspath("example.dll"))

# 定义函数参数和返回类型
lib.add.argtypes = [ctypes.c_int, ctypes.c_int]
lib.add.restype = ctypes.c_int

lib.print_string.argtypes = [ctypes.c_char_p]
lib.print_string.restype = None

lib.modify_array.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_int]
lib.modify_array.restype = None

# 调用函数
result = lib.add(5, 7)
print(f"5 + 7 = {result}")

# 调用字符串函数
lib.print_string(b"Hello from Python!")

# 调用数组处理函数
arr = (ctypes.c_double * 5)(1.1, 2.2, 3.3, 4.4, 5.5)
print("Original array:", [x for x in arr])
lib.modify_array(arr, len(arr))
print("Modified array:", [x for x in arr])